package com.JDK.Thread;

public class JoinDemo {


    final static JoinDemo lock = new JoinDemo();

    static volatile int count = 0;

    public static void main(String[] args) throws InterruptedException {
        Runnable r1 = () -> {
            for(int i = 0; i<50;i++){
                synchronized (lock){
                    doit(count++);
                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

        };

        Runnable r2 = () -> {
            for(int i = 0; i<50;i++){
                synchronized (lock){
                    doit(count++);
                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
    }

    public static void doit(int i){
        System.out.println(Thread.currentThread().getName() + " " + i);
    }
}
